package com.zy.fee.roadpark.activity.np;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.inputmethodservice.Keyboard;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.zy.fee.roadpark.R;
import com.zy.fee.roadpark.dao.NpNodeRecordLogic;
import com.zy.fee.roadpark.model.M_Node;
import com.zygk.library.model.CommonResult;
import com.zygk.library.util.HttpRequest;
import com.zygk.library.util.PreferencesHelper;
import com.zygk.library.util.StringUtils;
import com.zygk.library.util.ToastUtil;
import com.zygk.library.view.XKeyboardView;
import com.zygk.library.view.gridpasswordview.GridPasswordView;

/**
 * Created by Wikison on 2019/7/20.
 */
public class PlateFindActivity extends Activity {

  @BindView(R.id.gpvPlateNumber) GridPasswordView gpvPlateNumber;
  @BindView(R.id.view_keyboard) XKeyboardView viewKeyboard;

  private Context mContext;
  private String plateNumber = "";
  private PreferencesHelper preferencesHelper;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setFinishOnTouchOutside(true);
    setContentView(R.layout.activity_plate_find);
    ButterKnife.bind(this);
    getWindow().setGravity(Gravity.BOTTOM);
    //设置布局填充满宽度
    WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
    layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
    getWindow().setAttributes(layoutParams);

    init();
  }

  private void init() {
    initData();
    initView();
    initListener();
  }

  private void initData() {
    mContext = this;
    preferencesHelper = new PreferencesHelper(mContext);
  }

  private void initView() {

  }

  private void initListener() {
    viewKeyboard.setIOnKeyboardListener(new XKeyboardView.IOnKeyboardListener() {
      @Override public void onInsertKeyEvent(String text) {
        gpvPlateNumber.appendPassword(text);
      }

      @Override public void onDeleteKeyEvent() {
        gpvPlateNumber.deletePassword();
      }
    });

    gpvPlateNumber.togglePasswordVisibility();

    gpvPlateNumber.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
      @Override public boolean beforeInput(int position) {
        if (position == 0) {
          viewKeyboard.setKeyboard(new Keyboard(mContext, R.xml.provice));
          viewKeyboard.setVisibility(View.VISIBLE);
          return true;
        } else if (position >= 1 && position < 2) {
          viewKeyboard.setKeyboard(new Keyboard(mContext, R.xml.english));
          viewKeyboard.setVisibility(View.VISIBLE);
          return true;
        } else if (position >= 2 && position <= 7) {
          viewKeyboard.setKeyboard(new Keyboard(mContext, R.xml.qwerty_without_chinese));
          viewKeyboard.setVisibility(View.VISIBLE);
          return true;
        }
        viewKeyboard.setVisibility(View.GONE);
        return false;
      }

      @Override public void onTextChanged(String psw) {
      }

      @Override public void onInputFinish(String psw) {
      }
    });

    gpvPlateNumber.setOnFocusChangeListener((v, hasFocus) -> {
      if (!hasFocus) {
        viewKeyboard.setVisibility(View.GONE);
      }
    });
  }

  private void getNodeRecordID() {
    NpNodeRecordLogic.npnode_get_node_record_id(mContext,
        preferencesHelper.getSettingString(PreferencesHelper.User.Key.LOT_ID), plateNumber,
        new HttpRequest.HttpCallback() {
          @Override public void onStart() {

          }

          @Override public void onSucceed(Object obj) {
            CommonResult result = (CommonResult) obj;
            if (result.getStatus() == 1) {
              M_Node mNode = new M_Node();
              mNode.setCarNumber(plateNumber);
              mNode.setNodeRecordID(result.getNodeRecordID());
              Intent intent = new Intent(mContext, NpNodeReceivablesActivity.class);
              intent.putExtra(NpSpotListActivity.INTENT_NODE, mNode);
              startActivity(intent);
              finish();
            } else {
              ToastUtil.showMessage(mContext, result.getInfo());
            }
          }

          @Override public void onFailed() {

          }

          @Override public void onFinish() {

          }
        });
  }

  @OnClick({
      R.id.tv_no, R.id.tv_yes
  }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.tv_no:
        finish();
        break;
      case R.id.tv_yes:
        plateNumber = gpvPlateNumber.getPassWord();
        if (StringUtils.isBlank(plateNumber) || plateNumber.replace(" ", "").length() < 7) {
          ToastUtil.showMessage(mContext, "请输入完整车牌号！");
          return;
        } else {
          getNodeRecordID();
        }

        break;
    }
  }
}
